package com.mgm.creative.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mgm.creative.service.CreativeService;
import com.mgm.constant.GobalConstant;
import com.mgm.dao.CreativeMapper;
import com.mgm.page.DataGrid;
import com.mgm.pojo.Creative;
import com.mgm.pojo.CreativeExample;
import com.mgm.pojo.CreativeOfApp;
import com.mgm.pojo.CreativeOfAppExample;
import com.mgm.pojo.CreativeExample.Criteria;
import com.mgm.result.IMessage;
import com.mgm.utils.RandomCodeUtils;
import com.mgm.utils.UUIDUtil;

/**
 * 
 * @ClassName: CreativeServiceImpl 
 * @Description: TODO(...) 
 * @author Andy Zhang
 * @date 2018年6月28日 下午8:54:33 
 *
 */
@Service
public class CreativeServiceImpl implements CreativeService {
	
	@Autowired
	private CreativeMapper creativeMapper;
	
	@Override
	public IMessage addCreative(Creative creative) {
		if(StringUtils.isEmpty(creative.getOfferid())) {
			return IMessage.error("OfferId is not null!"); 
		}
		creative.setUid(UUIDUtil.uuidRep());
		creative.setEnabled(false);
		creativeMapper.insert(creative);
		return IMessage.success();
	}

	@Override
	public IMessage deleteCreative(Creative creative) {
		CreativeExample example = new CreativeExample();
		Criteria criteria = example.createCriteria();
		criteria.andOfferidEqualTo(creative.getOfferid());
		criteria.andUidEqualTo(creative.getUid());
		
		creativeMapper.deleteByExample(example);
		return IMessage.success();
	}

	@Override
	public IMessage deleteById(List<String> uids) {
		for (String uid : uids) {
			creativeMapper.deleteByPrimaryKey(uid);
		}
		return IMessage.success();
	}

	@Override
	public IMessage updateCreative(Creative creative) {
		creativeMapper.updateByPrimaryKeySelective(creative);
		return IMessage.success();
	}

	@Override
	public IMessage isEnabled(String uid, String offerid, Boolean enabled) {
		CreativeExample example = new CreativeExample();
		Criteria criteria = example.createCriteria();
		criteria.andOfferidEqualTo(offerid);
		criteria.andUidEqualTo(uid);
		List<Creative> list = creativeMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(list)) {
			return IMessage.error("操作失败");
		}
		
		Creative record = new Creative();
		record.setUid(uid);
		record.setEnabled(enabled);
		creativeMapper.updateByPrimaryKeySelective(record);
		return IMessage.success();
	}
	
	@Override
	public Creative selectById(String uid) {
		return creativeMapper.selectByPrimaryKey(uid);
	}

	@Override
	public List<Creative> findAllByEnabled() {
		CreativeExample example = new CreativeExample();
		Criteria criteria = example.createCriteria();
		criteria.andEnabledEqualTo((byte) 1);
		criteria.andUrlIsNotNull();
		return creativeMapper.selectByExample(example);
	}

	@Override
	public DataGrid findAll(String offerid, Integer pageNum, Integer pageSize) {
		//查询
		CreativeExample example = new CreativeExample();
		//分页处理
		PageHelper.startPage(pageNum, pageSize);
		//创建条件
		Criteria criteria = example.createCriteria();
		criteria.andOfferidEqualTo(offerid);
		
		List<Creative> list = creativeMapper.selectByExample(example);
		//创建一个返回值对象
		DataGrid dataGrid = new DataGrid();
		dataGrid.setItems(list);
		//获取PageInfo相关信息
		PageInfo<Creative> pageInfo = new PageInfo<>(list);
		dataGrid.setTotal_count(pageInfo.getTotal()); //总记录数
		dataGrid.setPage_count(pageInfo.getPages()); // 总页数
		dataGrid.setPage_index(pageInfo.getPageNum()); // 当前页
		
		return dataGrid;
	}
	
	@Override
	public void deleteByOfferIds(List<String> offerIds) {
		CreativeExample example = new CreativeExample();
		Criteria criteria = example.createCriteria();
		criteria.andOfferidIn(offerIds);
		creativeMapper.deleteByExample(example);
	}
	
	//offerid不能为空
	@Override
	public void deleteByCreative(List<Creative> list) {
		List<String> offerIds = new ArrayList<>();
		if(!CollectionUtils.isEmpty(list)) {
			for (Creative o : list) {
				offerIds.add(o.getOfferid());
			}
			deleteByOfferIds(offerIds);
		}
	}
	
	@Override
	public int insertBatch(List<Creative> list) {
		return creativeMapper.insertBatch(list);
	}

}
